package com.kmxd.ams.init;

import cn.hutool.core.util.StrUtil;
import com.kmxd.ams.app.oss.command.CheckTableCmd;
import com.kmxd.ams.app.system.command.InitCropCmd;
import com.kmxd.ams.client.system.ISysCropSvc;
import com.kmxd.ams.client.system.ISysFondsSvc;
import com.kmxd.ams.client.system.ISysRoleSvc;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.enums.DbTypeEnum;
import com.kmxd.ams.core.config.AmsConfig;
import com.kmxd.ams.core.exception.BizTipException;
import com.kmxd.ams.infra.system.entity.SysFonds;
import com.kmxd.ams.infra.tmpl.enums.TmplEnum;
import com.kmxd.ams.infra.tmpl.util.CreateTableUtils;
import com.kmxd.ams.infra.tmpl.util.TmplUtils;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * 系统启动后自动执行升级内容
 *
 * @author zuolg
 */
@Component
@Slf4j
public class InitUpgradeV4 {
  @Value("${app.upgrade}")
  private String upgrade;

  @Autowired private ISysRoleSvc sysRoleSvc;
  @Autowired private ISysCropSvc sysCropSvc;
  @Autowired private ISysFondsSvc sysFondsSvc;
  @Autowired private InitCropCmd initCropCmd;
  @Autowired private RedisTemplate redisTemplate;
  @Autowired private CheckTableCmd checkTableCmd;
  @Autowired private TmplUtils tmplUtils;
  @Autowired private AmsConfig amsConfig;
  @Autowired private CreateTableUtils ctu;
  @Autowired private JdbcTemplate jdbcTemplate;

  /** v4 升级业务行为 */
  public void creatLogEventTable() {
    List<SysFonds> fondsList = sysFondsSvc.list();
    String tableName = TmplEnum.log_archive.getName();
    fondsList.forEach(
        fonds -> {
          // 处理档案业务行为
          // 判断电子文件是否存在,// 新建电子文件表
          String tableNameLog = TmplEnum.log_archive.getCode() + "_" + fonds.getAutoIndex();
          int countLog = checkTableCmd.getTableCount(tableNameLog);
          if (countLog == 0) {
            tmplUtils
                .getTmpl(TmplEnum.log_archive.getCode())
                .forEach(
                    tmpl -> {
                      try {
                        String sql;
                        if (StrUtil.isBlank(amsConfig.getDbType())
                            || StrUtil.equals(DbTypeEnum.MYSQL.getCode(), amsConfig.getDbType())) {
                          sql = ctu.getTableSql(tableNameLog, tableName, tmpl.getFields());
                        } else if (StrUtil.equals(DbTypeEnum.PG.getCode(), amsConfig.getDbType())) {
                          sql = ctu.getTableSqlPg(tableNameLog, tableName, tmpl.getFields());
                        } else if (StrUtil.equals(DbTypeEnum.DM.getCode(), amsConfig.getDbType())) {
                          sql = ctu.getTableSqlDm(tableNameLog, tableName, tmpl.getFields());
                        } else {
                          throw BizTipException.instance(ErrorCode.VALIDATE_ERROR, "当前数据库类型为适配！");
                        }
                        // 暂时注释不添加
                        jdbcTemplate.execute(sql);
                      } catch (BizTipException e) {
                        log.error(e.getMessage(), e);
                      } catch (DataAccessException e) {
                        throw BizTipException.instance(
                            ErrorCode.VALIDATE_ERROR,
                            StrUtil.format("创建档案业务行为表异常!", e.getMessage()));
                      }
                    });
          }
        });
  }
}
